/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javase - T10
 * <p>Powered by webrx On 2021-12-24 17:01:49
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T10 {
    static CyclicBarrier bar = new CyclicBarrier(3, () -> {
        System.out.println("大家到起了,启动。");
        System.exit(0);
    });

    static void eat() {
        String tn = Thread.currentThread().getName();

        Random rand = new Random();
        int m = rand.nextInt(14) + 2;  //2 - 15
        System.out.println(tn + ":动身了,可能需要(" + m + ")秒到达.....");
        try {
            TimeUnit.SECONDS.sleep(m);
            System.out.println(tn + "：到达了。");
            bar.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Thread(T10::eat, "李四").start();
        new Thread(T10::eat, "赵六").start();
        //new Thread(T10::eat, "张三丰").start();
        //new Thread(T10::eat, "周强").start();
    }
}
